<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

/**
 * 聊天主逻辑
 * 主要是处理 onMessage onClose 
 */
use \GatewayWorker\Lib\Gateway;

class Events
{

    public static function onConnect($client_id)
    {
//        file_put_contents(__DIR__.'/test.log',date("Y-m-d H:i:s",time())."   ".$client_id."\r\n",FILE_APPEND);
        Gateway::sendToCurrentClient(json_encode(['type'=>'init','client_id'=>$client_id]));
    }
   
   /**
    * 有消息时
    * @param int $client_id
    * @param mixed $message
    */
   public static function onMessage($client_id,$message)
   {
       $message_data = json_decode($message, true);
       switch ($message_data['type']){
           case 'connect' :
               $room_id = $message_data['room_id'];
               $name = $message_data['name'];
               $_SESSION['room_id'] = $room_id;
               $_SESSION['name'] = $name;
               $_SESSION['token'] = $message_data['token'];
               file_put_contents(__DIR__.'/test.log',date("Y-m-d H:i:s",time())."   ".print_r($message_data,1)."\r\n",FILE_APPEND);
               //将进入房间的用户存在这个房间的组里
//               Gateway::joinGroup($client_id,$room_id);
//               GateWay::sendToGroup($room_id,json_encode(["type"=>"fedback","msg"=>"success"]));
               return;
           case 'reload' :
               $_SESSION['reload'] = $message_data['dataType'];
       }
   }
   
   /**
    * 当客户端断开连接时
    * @param integer $client_id 客户端id
    */
   public static function onClose($client_id)
   {
//       file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   ".$client_id."\r\n",FILE_APPEND);
        //通过curl将当前断开连接的client_id传递到框架那边做其他逻辑处理
       try{
           $uri = "http://ab.jdhui.com/chat2/del";
           $data = ["client_id"=>$client_id,"token"=>$_SESSION['token'],'reload'=>$_SESSION['reload']];
           //初始化
           file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time()).print_r($data,1)."\r\n",FILE_APPEND);
           $curl = curl_init();
           curl_setopt($curl, CURLOPT_URL, $uri);
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
           curl_setopt($curl, CURLOPT_POST, 1);
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
           $result = curl_exec($curl);
           if (curl_errno($curl)) {
//               file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   错误curl  ".curl_error($curl)."\r\n",FILE_APPEND);
               return  GateWay::sendToAll(json_encode(["dd"=>1111]));
           }
           curl_close($curl);
           if($result == NULL){
               $message['type'] = 12222;
//               file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   3131331231231"."\r\n",FILE_APPEND);
           }ELSE{
//               file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   22222"."\r\n",FILE_APPEND);
//               file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   ".print_r($result,1)."\r\n",FILE_APPEND);
               $output = json_decode($result,true);
               $message['type'] = $output;
           }

//           return  GateWay::sendToAll(json_encode($message));
       }catch (Exception $e){
//           file_put_contents(__DIR__.'/test.log2',date("Y-m-d H:i:s",time())."   ".print_r($e,1)."\r\n",FILE_APPEND);
       }
   }
  
}
